import React, { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";


function WhithRouter(Cpn: any) {
  const NewCpn: React.FC = () => {
    const { pathname } = useLocation();
    const navigate = useNavigate();
    const token = localStorage.getItem("token");

    useEffect(() => {
      if (!token && !["/login", "/register"].includes(pathname)) {
        navigate("/login");
      }
    }, [pathname,token,navigate]);

    return <Cpn />;
  };

  return NewCpn ;
}

export default WhithRouter;
